﻿// <copyright file="MainForm.cs" company="QUT">
//     Copyright (c) QUT. All rights reserved.
// </copyright>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LRT.Engine;

namespace LRT.GUI
{
    /// <summary>
    /// Main Form for Localisation Resource Toolkit
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// Place holder value for empty values.
        /// </summary>
        private const string NoValueText = "-";

        /// <summary>
        /// The Localisation Resource Tookit Engine Resource Generator.
        /// </summary>
        private ResourceGenerator _engine;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.SourceFileValueLabel.Text = MainForm.NoValueText;
            this.SourceCultureValueLabel.Text = MainForm.NoValueText;
            this.TargetFileValueLabel.Text = MainForm.NoValueText;
            var cultures = from c in CultureInfo.GetCultures(CultureTypes.AllCultures)
                           where c.LCID != 127 // Invariant Culture
                           select new { Value = c.Name, Text = string.Format("{0} ({1})", c.DisplayName, c.Name) };
            this.TargetCultureComboBox.ComboBox.ValueMember = "Value";
            this.TargetCultureComboBox.ComboBox.DisplayMember = "Text";
            this.TargetCultureComboBox.ComboBox.DataSource = cultures.ToArray();
            this.TargetCultureComboBox.ComboBox.SelectedIndex = -1;
            this.TargetCultureComboBox.ComboBox.SelectedIndexChanged += this.TargetCultureComboBox_SelectedIndexChanged;
            this.MainToolStripStatusLabel.Text = Strings.ReadyStatus;
        }

        /// <summary>
        /// Opens the resource.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OpenResource(object sender, EventArgs e)
        {
            this.MainToolStripStatusLabel.Text = Strings.LoadingStatus;
            if (this.MainOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.SourceFileValueLabel.Text = this.MainOpenFileDialog.FileName;
                var culture = new CultureInfo(ResourceGenerator.GetSourceCulture(this.MainOpenFileDialog.FileName));
                this.SourceCultureValueLabel.Text = string.Format("{0} ({1})", culture.DisplayName, culture.Name);
                this.LoadResources();
            }

            this.MainToolStripStatusLabel.Text = Strings.ReadyStatus;
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the TargetLocaleLanguageComboBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TargetCultureComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadResources();
        }

        /// <summary>
        /// Load the resources based on the selected source resource file and the target culture.
        /// </summary>
        private void LoadResources()
        {
            this.MainToolStripStatusLabel.Text = Strings.LoadingStatus;
            if (this.SourceCultureValueLabel.Text == MainForm.NoValueText || this.TargetCultureComboBox.SelectedIndex == -1)
            {
                return;
            }

            try
            {
                var settings = LRT.GUI.Properties.Settings.Default;
                this._engine = new ResourceGenerator(this.SourceFileValueLabel.Text, this.TargetCultureComboBox.ComboBox.SelectedValue.ToString(), settings.UseTranslationMemory, settings.UseGoogleTranslate);
                this.TargetFileValueLabel.Text = this._engine.TargetFilePath;
                this.ResourceDataGridView.DataSource = this._engine.Resources;
                this.SaveToolStripButton.Enabled = true;
                this.SaveToolStripMenuItem.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.MainToolStripStatusLabel.Text = Strings.ReadyStatus;
        }

        /// <summary>
        /// Saves the resource.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SaveResource(object sender, EventArgs e)
        {
            this.MainToolStripStatusLabel.Text = Strings.SavingStatus;
            this._engine.Save();
            this.MainToolStripStatusLabel.Text = Strings.ReadyStatus;
        }

        /// <summary>
        /// Shows the options.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ShowOptions(object sender, EventArgs e)
        {
            using (var frmOptions = new OptionsForm())
            {
                frmOptions.ShowDialog();
            }
        }

        /// <summary>
        /// Shows the about.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ShowAbout(object sender, EventArgs e)
        {
            using (var frmAbout = new AboutForm())
            {
                frmAbout.ShowDialog();
            }
        }

        /// <summary>
        /// Handles the DataBindingComplete event of the ResourceDataGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewBindingCompleteEventArgs"/> instance containing the event data.</param>
        private void ResourceDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            var settings = LRT.GUI.Properties.Settings.Default;
            foreach (DataGridViewRow row in this.ResourceDataGridView.Rows)
            {
                var source = (Engine.TargetValueSource)((int)row.Cells[3].Value);
                var colour = Color.White;
                switch (source)
                {
                    case Engine.TargetValueSource.TranslationMemory:
                        colour = settings.TranslationMemoryCellColour;
                        break;
                    case Engine.TargetValueSource.GoogleTranslate:
                        colour = settings.GoogleTranslateCellColour;
                        break;
                }

                foreach (DataGridViewCell cell in row.Cells)
                {
                    cell.Style.BackColor = colour;
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the ExitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
